#include "EGL/EGLWin32.h"
#include "Win32/WindowsUtilities.h"

namespace Nezha
{
	void EGLWin32::_uninitialize()
	{
		EGLAdaptor::_uninitialize();
	}

	NZ_NativeWindow EGLWin32::createWindow(EngineConfig& cfg)
	{
		NezhaEngine* engine = NezhaEngine::getSingletonPtr();

		if(mhWnd)
		{
			engine->_destroyNativeWindow();
			mhWnd = NULL;
		}

		mhWnd = (EGLNativeWindowType)engine->_createNativeWindow(cfg);

		cfg.mWindow = (NZ_NativeWindow)mhWnd;
		mNativeDisplay = GetDC(mhWnd);
		mGLDisplay = eglGetDisplay(mNativeDisplay);

		if(mGLDisplay == EGL_NO_DISPLAY)
		{
			mGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
		}

		if(eglInitialize(mGLDisplay, NULL, NULL) == EGL_FALSE)
		{
			NEZHA_EXCEPTION(NZException::InvalidState, "Couldn't initialize EGLDisplay from native display.");
		}

		return cfg.mWindow;
	}

	void EGLWin32::destroyWindow(NZ_NativeWindow wnd)
	{
		// TODO	
	}

#ifdef WIN32

	GLESAdaptor* GLESAdaptor::CreateInstance()
	{
		return static_cast<GLESAdaptor*>(new EGLWin32);
	}

	NativeDisplayType InternalGetNativeDisplay(EGLNativeWindowType wnd)
	{
		if(wnd)
		{
			return GetDC(wnd);
		}

		return EGL_DEFAULT_DISPLAY;
	}

#endif //end WIN32

}//end namespace Nezha